#!/bin/bash
if [ "$(uname)" = "Darwin" ]; then
	CMD_READLINK="greadlink"
	CMD_DIRNAME="gdirname"
else
	CMD_READLINK="readlink"
	CMD_DIRNAME="dirname"
fi

_SELF=$("${CMD_READLINK}" -f "$0")
_SELF_BASE=$("${CMD_DIRNAME}" "${_SELF}")

if [ -z "${_SELF}" ]; then
	echo "Failed to detect fully qualified path of '$0'" 1>&2
	exit 2
fi
if [ -z "${_SELF}" ] && [ ! -d "${_SELF_BASE}" ]; then
	echo "Failed to detect fully qualified base directory of '$0'" 1>&2
	exit 2
fi

_CHECKPATCHPL="${_SELF_BASE}/checkpatch.pl"
if [ ! -x "${_CHECKPATCHPL}" ]; then
	echo "Failed to find 'checkpatch.pl' under '${_SELF_BASE}'" 1>&2
	exit 2
fi

_CHECKPATCHCONF=
if [ -f "${_SELF_BASE}/../../.checkpatch.conf" ]; then
	_CHECKPATCHCONF=($(cat "${_SELF_BASE}/../../.checkpatch.conf"))
fi

exec "${_CHECKPATCHPL}" "${_CHECKPATCHCONF[@]}" "$@"
